class UI {
	static _defaultMsg() {
		return UI._card([
			UI._header("Something went wrong, Please contact AYi 😭")
		])
	}

	static _card(modules) {
		modules = modules.filter(m => m);
		return [{
			type: "card",
			size: "lg",
			color: "#39c5bb",
			modules
		}]
	}
	static _header(content) {
		content = content ?? "Something went wrong, Please contact AYi 😭";
		return {
			type: "header",
			text: {
				type: "plain-text",
				content
			}
		};
	}
	static _divider(visible) {
		if(visible) return {
			type: "divider"
		}
	}
	static _countdown(endTime, mode) {
		const now = Date.now();
		endTime = now + (endTime ?? 10);
		return {
			type: "countdown",
			mode: mode ?? "second",
			startTime: now,
			endTime
		}
	}
	static _button(content, theme, value, visible) {
		if (visible) return {
			type: "button",
			theme,
			value,
			click: "return-val",
			text: {
				type: "plain-text",
				content
			}
		}
	}
	static _actionGroup(elements, visible) {
		elements = elements.filter(e => e);
		if(visible) return {
			type: "action-group",
			elements
		}
	}
	static _section(content, pic_url, mode) {
		return {
			type: "section",
			text: {
				type: "kmarkdown",
				content
			},
			mode: mode ?? "right",
			accessory: {
				type: "image",
				src: pic_url,
				size: "sm"
			}
		}
	}

	static getPlaylistFormat(playlist_arr, music_now, limit) {

		if (!playlist_arr || !music_now) return UI._defaultMsg();
		if (!music_now.music || !music_now.music.name) return UI._defaultMsg();
		if (!playlist_arr.length) return UI._defaultMsg();
		let data = [{
			type: "card",
			size: "lg",
			color: "#39c5bb",
			modules: [{
				type: "header",
				text: {
					type: "plain-text",
					content: `੭ ᐕ)੭✧ 正在播放: ${music_now.music.name}`
				}
			},
			UI._divider()
			]
		}];

		for (let i = music_now.index, flag = limit ?? 10; i < playlist_arr.length; i++) {
			if (flag-- < 0) break;
			data[0].modules.push({
				type: "section",
				mode: "right",
				accessory: {
					type: "button",
					theme: "danger",
					value: JSON.stringify({
						action: "playlist-remove",
						musicId: playlist_arr[i].id,
					}),
					click: "return-val",
					text: {
						type: "plain-text",
						content: "移除"
					}
				},
				text: {
					type: "kmarkdown",
					content: `**${playlist_arr[i].name}** \n${playlist_arr[i].artists.map(item => { return item.name }).join('/')}\t${playlist_arr[i].VIP ? '(font)VIP歌曲(font)[warning]' : ''}`
				}
			})
		};
		return data;
	}

	static getNowPlayingFormat(music_now) {
		let content;
		return content;
	}

	static getPopularityFormat(popularity) {
		if (!popularity || typeof popularity !== 'number') return "🌑🌑🌑🌑🌑";
		if (popularity >= 100) return "🌕🌕🌕🌕🌕";
		popularity /= 5;
		const map = ["🌑", "🌘", "🌗", "🌖", "🌕"];
		let ret = '🌕'.repeat(Math.floor(popularity / 4));
		ret += map[Math.round(popularity % 4)];
		ret += '🌑'.repeat(5 - ret.length / 2);
		return ret;
	}

	constructor(emitter) {
		this.config = {
			paused: false,
			folded: true,
			mode:1
		}
		this.emitter = emitter;
	}

	configHandler(value) {
		const mode_map = ["loop", "random", "single", "once"];
		let emit = true;
		switch (value.action) {
			case "stop":
				this.config.paused = true; break;
			case "start":
				this.config.paused = false; break;
			case "unfold-more":
				this.config.folded = false; break;
			case "fold-more":
				this.config.folded = true; break;
			// case "like":
			// 	this.config.like = !this.config.like; break;
			case "unfold-playlist":
				this.config.playlist = false; break;
			case "fold-playlist":
				this.config.playlist = true; break;
			case "play-mode":
				this.config.mode = mode_map.indexOf(value.mode);
				this.config.mode = this.config.mode < 0 ? 0 : this.config.mode;
				break;
			default:
				emit = false;
		}
		if (emit) this.emitter.emit("UI_CHANGE");
	}

	getMainPlayerFormat(music_now) {
		if (!music_now || !music_now.music || !music_now.music.name) return UI._defaultMsg();

		const paused =   this.config.paused   ? true : false;
		const folded =   this.config.folded   ? true : false;
		const playlist = this.config.playlist ? true : false;
		const liked  = false;
		const playmode = this.config.mode ?? 0;
		const music = music_now.music;

		return UI._card([
			UI._header(`੭ ᐕ)੭✧ 正在播放: ${music.name}`),
			UI._section(
				`**(font)—✧ ${music.name} ✧—(font)[warning]**\t${music.VIP ? `(font)VIP歌曲(font)[warning]\t` : `[下载](${music.url})`}\n(font)歌手:(font)[pink] ${music.artists.map(a => a.name).join('/').substring(0, 20)}\t\n(font)热度:  (font)[pink]${UI.getPopularityFormat(music.popularity)}`,
				music.album.picUrl
			),
			UI._countdown(music.duration),
			UI._actionGroup([
				UI._button("  上 一 首  ", "primary", JSON.stringify({ action: "play-prev" }),	  true),
				UI._button("  停 止  ",    "danger",  JSON.stringify({ action: "stop" }), 	!paused),
				UI._button("  开 始  ",    "primary", JSON.stringify({ action: "start" }),   paused),
				UI._button("  下 一 首  ", "primary", JSON.stringify({ action: "play-next" }), 	  true),
				UI._button("展开更多操作",  "warning", JSON.stringify({ action: "unfold-more" }),  folded),
				UI._button("收起更多操作",  "warning", JSON.stringify({ action: "fold-more" }),	  !folded),
			], true),
			UI._divider(),
			UI._actionGroup([
				[	UI._button(" 列表循环 ", "info", JSON.stringify({ action: "play-mode", mode: "loop" }),   true),
				 	UI._button(" 随机播放 ", "info", JSON.stringify({ action: "play-mode", mode: "random" }), true),
				 	UI._button(" 单曲循环 ", "info", JSON.stringify({ action: "play-mode", mode: "single" }), true),
				 	UI._button(" 顺序播放 ", "info", JSON.stringify({ action: "play-mode", mode: "once" }),   true)
				][playmode],
				UI._button("  收 藏  ", "primary", JSON.stringify({ action: "like" }), 	!liked),
				UI._button("  已 收 藏  ", "info",   JSON.stringify({ action: "dislike" }),	 liked),
				UI._button("  什 么 呢  ", "primary", JSON.stringify({ action: "play-next" }), true),
				UI._button("展开播放列表", "warning", JSON.stringify({ action: "unfold-playlist" }), !playlist),
				UI._button("收起播放列表", "warning", JSON.stringify({ action: "fold-playlist" }),	  playlist),
			], !folded),
			UI._divider(!folded)
		])
	}
}

module.exports = UI;